﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLFacade;
using BLFacade.entities;
using System.Data.Objects;

namespace BL
{
    public class DataFacade : IDataFacade
    {
        Entities entities;
        public DataFacade()
        {
            entities = new Entities();
        }

        public List<IEmployee> GetEmployees()
        {
            return entities.Employees.ToList<IEmployee>();
        }

        public List<ICustomer> GetCustomers()
        {
            return entities.Customers.ToList<ICustomer>();
        }


        public ICustomerContacts GetContact(string FirstName, string MiddleName, string LastName, ICustomer customer)
        {
            return entities.CustomerContacts.First(p => p.FirstName == FirstName && p.MiddleName == MiddleName &&
                                                    p.LastName == LastName && p.Customer.Name == customer.Name);
        }


        public void SaveChanges()
        {
            entities.SaveChanges();
        }


        public void CreateCustomer(string name, string address, string description)
        {
            entities.AddToCustomers(new Customer() 
            {   Name = name, 
                Address = address, 
                Description = description 
            });
        }


        public List<IProject> GetProjects()
        {
            return entities.Projects.ToList<IProject>();
        }


        public List<IAppointment> GetAppointments()
        {
            return entities.Appointments.ToList<IAppointment>();
        }


        public void CreateEmployee(string firstName, string middleName, string lastName, string email, string phoneNumber, DateTime dateofBirth, IAppointment appointment, double salaryModifier, string username, string password)
        {
            entities.AddToEmployees(new Employee()
                {
                    Person = (Person)CreatePerson(firstName, middleName, lastName, email, phoneNumber, username, password),
                    DateofBirth = dateofBirth,
                    AcceptDate = DateTime.Now,
                    SalaryModifier = salaryModifier,
                    Appointment = (Appointment)appointment
                });
        }


        public IPerson CreatePerson(string firstName, string middleName, string lastName, string email, string phoneNumber, string username, string password)
        {
            Person person = new Person()
            {
                FirstName = firstName,
                MiddleName = middleName,
                LastName = lastName,
                Email = email,
                PhoneNumber = phoneNumber,
                Credentials = new Credentials() { Username = username, Password = password }
            };
            entities.AddToPersons(person);
            return person;
        }


        public bool IsUniqueUsername(string username)
        {
            return entities.Persons.Any(p => p.Credentials.Username == username);
        }
    }
}
